package net.takela.common.web.service.sys;


import net.takela.common.web.dao.sys.SysConfDao;
import net.takela.common.web.model.sys.SysConf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author baboy
 * create at 07/12/2016
 */
@Service("sysConfService")
public class SysConfService {
    @Autowired
    protected SysConfDao sysConfDao;
    @Autowired
    @Qualifier("memoryCacheService")
    private MemoryCacheService cacheService;
    private static final String CACHE_KEY = "sys_conf_cache_key";

    private Integer cacheExpireTimeInSeconds = 60;
    /**
     * 
     */
    public Integer getCacheExpireTimeInSeconds() {
        return cacheExpireTimeInSeconds;
    }
    /**
     * 
     */
        public void setCacheExpireTimeInSeconds(Integer cacheExpireTimeInSeconds) {
        this.cacheExpireTimeInSeconds = cacheExpireTimeInSeconds;
    }
    /**
     * 
     */
        public Integer add(SysConf conf) {
        return sysConfDao.add(conf);
    }
    /**
     * 
     */
        public boolean remove(Integer id) {
        return sysConfDao.remove(id);
    }
    /**
     * 
     */
        public boolean set(SysConf conf) {
        if(conf.getId() == null || conf.getId() <= 0){
            SysConf c = getByKey(conf.getKey());
            if(c==null){
                sysConfDao.add(conf);
                return true;
            }
            conf.setId(c.getId());
        }
        return sysConfDao.update(conf);
    }
    /**
     * 
     */
        public List<SysConf> getAllConfs() {
        List<SysConf> confs = (List<SysConf>)cacheService.get( CACHE_KEY );
        if (confs == null){
            confs = sysConfDao.getAllConfs();
            //缓存
            cacheService.set(CACHE_KEY, confs, cacheExpireTimeInSeconds);
        }
        return confs;
    }
    /**
     * 
     */
        public List<SysConf> getAllConfs(String module) {
        List<SysConf> list = getAllConfs();

        List<SysConf> confs = new ArrayList<>();
        for (int i = 0; i< list.size(); i++){
            SysConf conf = list.get(i);
            if(module == null || module.equalsIgnoreCase(conf.getModule()) ){
                confs.add(conf);
            }
        }

        return confs.size() > 0 ? confs : null;
    }
    /**
     * 
     */
        public SysConf get(Integer id) {
//        List<SysConf> list = getAllConfs();
//        for (SysConf conf : list){
//            if (conf.getId().equals(id)){
//                return conf;
//            }
//        }
        SysConf conf = sysConfDao.get(id);
        return conf;
    }
    /**
     * 
     */
        public SysConf getByKey(String key) {
        SysConf conf = sysConfDao.getByKey(key);
        return conf;
//        List<SysConf> list = getAllConfs();
//        for (SysConf conf : list){
//            if (key.equals(conf.getKey())){
//                return conf;
//            }
//        }
//        return null;
    }
    /**
     * 
     */
        public Object getValue(String key) {
        SysConf conf = getByKey(key);
        if(conf == null){
            return null;
        }
        return conf.getTypedValue();
    }
    /**
     * 
     */
        public Map getMapValue(String key) {
        SysConf conf = getByKey(key);
        if(conf == null){
            return null;
        }
        return conf.getMapValue();
    }
    /**
     * 
     */
        public List getListValue(String key) {
        SysConf conf = getByKey(key);
        if(conf == null){
            return null;
        }
        return conf.getListValue();
    }
    /**
     * 
     */
        public Long getLongValue(String key) {

        SysConf conf = getByKey(key);
        if(conf == null){
            return null;
        }
        return conf.getLongValue();
    }
    /**
     * 
     */
        public Integer getIntegerValue(String key) {

        SysConf conf = getByKey(key);
        if(conf == null){
            return null;
        }
        return conf.getIntegerValue();
    }
    /**
     * 
     */
        public Float getFloatValue(String key) {
        SysConf conf = getByKey(key);
        if(conf == null){
            return null;
        }
        return conf.getFloatValue();
    }
}
